//
//  RegisterManager.swift
//  SeeFM_Swift
//
//  Created by CBCT_MBP on 2021/6/23.
//
//  设备注册信息管理

let kRegisterInfo: String = "kRegisterInfoKey"
let kLocationInfo: String = "kLocationInfoKey"
let kCid: String = "kCidKey"


import UIKit

struct TimeModel: HandyJSON {
    var time: Int?
}


class RegisterManager: NSObject {
    
    static let shared = RegisterManager()
    
    public func isRegister() -> Bool {
        return UserDefaults.standard.object(forKey: kRegisterInfo) != nil
    }
    
    public func saveRegisterDeviceInfo(registerModel : RegisterModel?) {
        guard let json = registerModel?.toJSON() else { return }
        UserDefaults.standard.set(json, forKey: kRegisterInfo)
    }
    
    public func getRegisterInfo() -> RegisterModel? {
        let info = UserDefaults.standard.object(forKey: kRegisterInfo)
        if info != nil {
            let model = RegisterModel.deserialize(from: info as? [String :Any])
            return model
        }else {
            return nil
        }
    }
    
    public func getLocationInfo() -> LocationModel? {
        let info = UserDefaults.standard.object(forKey: kLocationInfo)
        let json = String.init(data: info as! Data, encoding: .utf8)
        let model = LocationModel.deserialize(from: json)
        return model
    }

    public func getCid() -> String {
        let cid = UserDefaults.standard.string(forKey: kCid)
        return cid ?? ""
    }
    
    public func saveLocationInfo(location: LocationModel?) {
        guard let json = location?.toJSONString else { return }
        UserDefaults.standard.setValue(json, forKey: kLocationInfo)
    }
    
    public func saveCid(cid: String?) {
        guard let tid = cid else { return }
        UserDefaults.standard.setValue(tid, forKey: kCid)
    }
    
    
    // 注册设备信息
    fileprivate var isRegistering: Bool = false
    func register(_ res: ((_ result: Bool)->())?) {
        
        let model = RegisterManager.shared.getRegisterInfo()
        if model != nil {
            res?(true)
            return
        }
        
        if isRegistering {return}
        
        isRegistering = true
        
        let param = [
            "device_id":UIDevice.current.uuidStr(),
            "imei":UIDevice.current.imeiStr(),
            "idfa":SFNetBean.shared.getIdfa(),
            "mid":"020000000000000",
            "device":SFNetBean.shared.systemHardwareName(),
            "os":"iOS",
            "os_version":SFNetBean.shared.systemVersion(),
            "app_version":SFNetBean.appVersion,
            "channel_id":Adapter.Param.get(kind: .App_channel),
            "ip":SFNetBean.shared.getIFAddresses(),
            "longitude":"-1",
            "latitude":"-1",
        ]

        SFNetwork.post(url: kAPI_register_device, params: param) { response in
            
            switch response {
            case .success(let result):
                
                let model = RegisterModel.deserialize(from: result.data!["d"] as? [String:Any])
                RegisterManager.shared.saveRegisterDeviceInfo(registerModel: model)
                break
                
            case .fail(let error):
                print(error)
                break
                
            }
            
            self.isRegistering = false
        }
    }
}
